<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="所属单位" prop="companyCode">
        <el-input
          v-model="queryParams.companyCode"
          placeholder="请输入所属单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="monitorName">
        <el-input
          v-model="queryParams.monitorName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="monitor_code" prop="monitorCode">
        <el-input
          v-model="queryParams.monitorCode"
          placeholder="请输入monitor_code"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属系统(字典fire_system_type)" prop="system">
        <el-input
          v-model="queryParams.system"
          placeholder="请输入所属系统(字典fire_system_type)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备厂商（字典cfs_data_equipment_manufacturer_type）" prop="dockCode">
        <el-input
          v-model="queryParams.dockCode"
          placeholder="请输入设备厂商（字典cfs_data_equipment_manufacturer_type）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备类型（关联cfs_data_manufactor_device表device_code字段）" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备类型（关联cfs_data_manufactor_device表device_code字段）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备型号（关联cfs_data_manufactor_device表id字段）" prop="monitorType">
        <el-select
          v-model="queryParams.monitorType"
          placeholder="请选择设备型号（关联cfs_data_manufactor_device表id字段）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="运营商（1中国电信 2中国移动 3中国联通）" prop="commOperator">
        <el-input
          v-model="queryParams.commOperator"
          placeholder="请输入运营商（1中国电信 2中国移动 3中国联通）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="通讯协议（TCP、UDP、HTTP）" prop="commProtocol">
        <el-input
          v-model="queryParams.commProtocol"
          placeholder="请输入通讯协议（TCP、UDP、HTTP）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="IMEI" prop="imei">
        <el-input
          v-model="queryParams.imei"
          placeholder="请输入IMEI"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="iccid" prop="iccid">
        <el-input
          v-model="queryParams.iccid"
          placeholder="请输入iccid"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="装机日期" prop="installDate">
        <el-date-picker
          v-model="queryParams.installDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="安装位置" prop="installAddress">
        <el-input
          v-model="queryParams.installAddress"
          placeholder="请输入安装位置"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="入网时间" prop="networkDate">
        <el-date-picker
          v-model="queryParams.networkDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="服务到期时间" prop="servicedDate">
        <el-date-picker
          v-model="queryParams.servicedDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="运行状态（0正常 1故障 2报警）" prop="runState">
        <el-input
          v-model="queryParams.runState"
          placeholder="请输入运行状态（0正常 1故障 2报警）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="经度" prop="lng">
        <el-input
          v-model="queryParams.lng"
          placeholder="请输入经度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="lat">
        <el-input
          v-model="queryParams.lat"
          placeholder="请输入纬度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属消防网格" prop="fireGridId">
        <el-input
          v-model="queryParams.fireGridId"
          placeholder="请输入所属消防网格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="监测的设备类型（字典cfs_data_monitor_type）" prop="monitoringType">
        <el-select
          v-model="queryParams.monitoringType"
          placeholder="请选择监测的设备类型（字典cfs_data_monitor_type）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="对接app_id" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入对接app_id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第三方数据id" prop="dataId">
        <el-input
          v-model="queryParams.dataId"
          placeholder="请输入第三方数据id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入修改人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="安装用户类型(关联字典monitor_install_user_type)" prop="installUserType">
        <el-select
          v-model="queryParams.installUserType"
          placeholder="请选择安装用户类型(关联字典monitor_install_user_type)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cfs:data-nb-usermonitorinfo:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cfs:data-nb-usermonitorinfo:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="所属单位" align="center" prop="companyCode" />
      <el-table-column label="设备名称" align="center" prop="monitorName" />
      <el-table-column label="monitor_code" align="center" prop="monitorCode" />
      <el-table-column label="所属系统(字典fire_system_type)" align="center" prop="system" />
      <el-table-column label="设备厂商（字典cfs_data_equipment_manufacturer_type）" align="center" prop="dockCode" />
      <el-table-column label="设备类型（关联cfs_data_manufactor_device表device_code字段）" align="center" prop="deviceCode" />
      <el-table-column label="设备型号（关联cfs_data_manufactor_device表id字段）" align="center" prop="monitorType" />
      <el-table-column label="运营商（1中国电信 2中国移动 3中国联通）" align="center" prop="commOperator" />
      <el-table-column label="通讯协议（TCP、UDP、HTTP）" align="center" prop="commProtocol" />
      <el-table-column label="IMEI" align="center" prop="imei" />
      <el-table-column label="iccid" align="center" prop="iccid" />
      <el-table-column
        label="装机日期"
        align="center"
        prop="installDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="安装位置" align="center" prop="installAddress" />
      <el-table-column
        label="入网时间"
        align="center"
        prop="networkDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="服务到期时间"
        align="center"
        prop="servicedDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="运行状态（0正常 1故障 2报警）" align="center" prop="runState" />
      <el-table-column label="经度" align="center" prop="lng" />
      <el-table-column label="纬度" align="center" prop="lat" />
      <el-table-column label="所属消防网格" align="center" prop="fireGridId" />
      <el-table-column label="监测的设备类型（字典cfs_data_monitor_type）" align="center" prop="monitoringType" />
      <el-table-column label="对接app_id" align="center" prop="appId" />
      <el-table-column label="第三方数据id" align="center" prop="dataId" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="修改人" align="center" prop="updateUser" />
      <el-table-column label="安装用户类型(关联字典monitor_install_user_type)" align="center" prop="installUserType" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cfs:data-nb-usermonitorinfo:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cfs:data-nb-usermonitorinfo:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DataNbUsermonitorinfoForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DataNbUsermonitorinfoApi, DataNbUsermonitorinfoVO } from '@/api/cfs/datanbusermonitorinfo'
import DataNbUsermonitorinfoForm from './DataNbUsermonitorinfoForm.vue'

/** 联网设备NB类型 列表 */
defineOptions({ name: 'DataNbUsermonitorinfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DataNbUsermonitorinfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  companyCode: undefined,
  monitorName: undefined,
  monitorCode: undefined,
  system: undefined,
  dockCode: undefined,
  deviceCode: undefined,
  monitorType: undefined,
  commOperator: undefined,
  commProtocol: undefined,
  imei: undefined,
  iccid: undefined,
  installDate: [],
  installAddress: undefined,
  networkDate: [],
  servicedDate: [],
  runState: undefined,
  lng: undefined,
  lat: undefined,
  fireGridId: undefined,
  monitoringType: undefined,
  appId: undefined,
  dataId: undefined,
  status: undefined,
  createTime: [],
  createUser: undefined,
  updateUser: undefined,
  installUserType: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DataNbUsermonitorinfoApi.getDataNbUsermonitorinfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DataNbUsermonitorinfoApi.deleteDataNbUsermonitorinfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DataNbUsermonitorinfoApi.exportDataNbUsermonitorinfo(queryParams)
    download.excel(data, '联网设备NB类型.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>